package zhh.tangbao.auxiliary.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zhh.tangbao.auxiliary.dao.SystemNoticeDao;
import zhh.tangbao.auxiliary.dto.notice.NoticeQueryDto;
import zhh.tangbao.auxiliary.entity.SystemMessage;
import zhh.tangbao.auxiliary.entity.SystemNotice;
import zhh.tangbao.auxiliary.service.SystemNoticeService;
import zhh.tangbao.system.constant.DataStatus;
import zhh.tangbao.system.utils.UserHolderUtil;
import zhh.tangbao.user.dao.UserDao;
import zhh.tangbao.user.vo.UserAccountAndNameVo;
import zhh.tangbao.user.vo.UserVo;
import zhh.tangbao.websocket.WebSocketServiceType;
import zhh.tangbao.websocket.pojo.WebSocketMessageEntity;
import zhh.tangbao.websocket.service.WebSocketService;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 2023/2/26
 * @author 粥灰灰
 * @version 1.0
 * 系统公告服务层
 */

@Service
public class SystemNoticeServiceImpl implements SystemNoticeService {

    @Autowired
    private UserHolderUtil userHolderUtil;

    @Autowired
    private SystemNoticeDao systemNoticeDao;

    @Autowired
    private UserDao userDao;


    @Resource(name = WebSocketServiceType.MESSAGE)
    private WebSocketService messageService;

    @Override
    @Transactional
    public int releaseSystemNotice(SystemNotice systemNotice) {
        UserVo currentUser = userHolderUtil.getCurrentUser();
        systemNotice.setBaseCreateInfo(currentUser.getId(), DataStatus.ENABLE);
        int insert = systemNoticeDao.insert(systemNotice);
        WebSocketMessageEntity e = new WebSocketMessageEntity();
        e.setTitle("发布公告");
        e.setContent(currentUser.getUserName() + "发布了公告");
        messageService.sendMessageToAll(currentUser.getId(), e);
        return insert;
    }

    @Override
    public SystemNotice get(String id) {
        SystemNotice notice = systemNoticeDao.selectById(id);
        Assert.notNull(notice, "公告不存在");
        return notice;
    }

    @Override
    public Page<SystemNotice> page(NoticeQueryDto dto) {
        Page<SystemNotice> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        QueryWrapper<SystemNotice> query = new QueryWrapper<>();
        query.eq("data_status", DataStatus.ENABLE);
        query.orderByDesc("record_time");
        if (StrUtil.isNotBlank(dto.getTitle())) {
            query.like("notice_title", dto.getTitle());
        }
        if (null != dto.getStart() && null != dto.getEnd()) {
            query.between("record_time", dto.getStart(), dto.getEnd());
        }
        page = systemNoticeDao.selectPage(page, query);
        List<SystemNotice> records = page.getRecords();
        if (CollUtil.isEmpty(records)) {
            return page;
        }
        List<UserAccountAndNameVo> userAccountAndNameById = userDao.getUserAccountAndNameById(records.stream().map(SystemNotice::getRecordUserId).collect(Collectors.toList()));
        if (CollUtil.isEmpty(userAccountAndNameById)) {
            return page;
        }
        Map<String, UserAccountAndNameVo> collect = userAccountAndNameById.stream().collect(Collectors.toMap(UserAccountAndNameVo::getId, Function.identity()));
        for (int i = 0; i < records.size(); i++) {
            SystemNotice m = records.get(i);
            UserAccountAndNameVo u = collect.get(m.getRecordUserId());
            if (u != null) {
                m.setRecordUserName(u.getUserName());
            }
        }
        return page;
    }

    @Override
    public int delete(String id) {
        SystemNotice notice = systemNoticeDao.selectById(id);
        Assert.notNull(notice, "公告不存在或已被删除");
        notice.setUpdateInfo(userHolderUtil.getCurrentUser().getId(), DataStatus.DELETED);
        return systemNoticeDao.updateById(notice);
    }
}
